package com.fhz.day002;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayMap;
import androidx.databinding.ViewDataBinding;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.fhz.day002.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    User xiaoming;
    User2 user2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        xiaoming  = new User();
        xiaoming.name.set("xiaoming");
        xiaoming.age.set(19);
        binding.setUser(xiaoming);

        List<String> list  = new ArrayList<>();
        list.add("one");
        list.add("two");

        binding.setList(list);
        binding.setIndex(1);

        binding.setActivity(this);

//        ViewDataBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
//        viewDataBinding.setVariable(BR.user,xiaoming);


        //可观察的集合
        ObservableArrayMap<String, Object> map = new ObservableArrayMap<>();
        map.put("firstName", "Google");
        map.put("lastName", "Inc.");
        map.put("age", 17);
        binding.setMap(map);


        //可变对象
        user2 = new User2();
        user2.setFirstName("user2的名字");
        user2.setLastName("user2的last名字");
        binding.setUser2(user2);

    }

    public void onClick(View view){

        switch (view.getId()){
            case R.id.click_one_test:
                Toast.makeText(this, "one", Toast.LENGTH_SHORT).show();
                xiaoming.name.set("20");
                break;
            case R.id.click_two_test:
                Toast.makeText(this, "two", Toast.LENGTH_SHORT).show();
                user2.setFirstName("自动变化");
                break;
        }

    }
}